<?php

/**
 * @author phuocndt
 * @copyright 2010
 */
    
/**
 * Upload file zip to widgets
 * */
    $target_path = "../widgets/";
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
        echo "The file ".  basename( $_FILES['uploadedfile']['name'])." has been uploaded.<br />";
    }

/**
 * Extract file zip to widgets
 * */
function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true)
{
  if(function_exists("zip_open"))
  {  
      if(!is_resource(zip_open($src_file)))
      {
          $src_file=dirname($_SERVER['SCRIPT_FILENAME'])."/".$src_file;
      }
     
      if (is_resource($zip = zip_open($src_file)))
      {         
          $splitter = ($create_zip_name_dir === true) ? "." : "/";
          if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";
          
          // Create the directories to the destination dir if they don't already exist
          create_dirs($dest_dir);

          // For every file in the zip-packet
          while ($zip_entry = zip_read($zip))
          {
            // Now we're going to create the directories in the destination directories
          
            // If the file is not in the root dir
            $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
            if ($pos_last_slash !== false)
            {
              // Create the directory where the zip-entry should be saved (with a "/" at the end)
              create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
            }

            // Open the entry
            if (zip_entry_open($zip,$zip_entry,"r"))
            {
            
              // The name of the file to save on the disk
              $file_name = $dest_dir.zip_entry_name($zip_entry);
            
              // Check if the files should be overwritten or not
              if ($overwrite === true || $overwrite === false && !is_file($file_name))
              {
                // Get the content of the zip entry
                $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));          
               
                if(!is_dir($file_name))           
                file_put_contents($file_name, $fstream );
                // Set the rights
                if(file_exists($file_name))
                {
                    chmod($file_name, 0777);
                    echo "<span style=\"color:#1da319;\">file saved: </span>".$file_name."<br />";
                }
                else
                {
                    echo "<span style=\"color:red;\">file not found: </span>".$file_name."<br />";
                }
              }
            
              // Close the entry
              zip_entry_close($zip_entry);
            }     
          }
          // Close the zip-file
          zip_close($zip);
          unlink($src_file);
      }
      
      /** phuocndt - Read xml insert database
       * C?t du?ng d?n c?a widgets vào m?ng
       * G?i DOM c?a php
       * Ki?m tra n?u t?n t?i (dang install) thì load file xml
       * Ch?y vòng l?p l?y t?t c? tag name gán vào bi?n
       * Insert vào database các bi?n l?y du?c
       */
      $wid_name = explode("/", substr($src_file, 0, strrpos($src_file, $splitter)));
      //print_r($wid_name);
      $objDOM = new DOMDocument();
      if($wid_name[2]){
          $objDOM->load($dest_dir.'p_widgets_'.$wid_name[2].'.xml');
          
          $widgets = $objDOM->getElementsByTagName("widgets");
          foreach( $widgets as $value ){
            $name = $value->getElementsByTagName("name");
            $task  = $name->item(0)->nodeValue;    
            $description = $value->getElementsByTagName("description");
            $detail  = $description->item(0)->nodeValue;    
            //echo "$task :: $detail <br>";
          }
          // Import widgets information into database
          tep_db_query("insert into ushop_widgets(name,description,folders)values('$task','$detail','$dest_dir')");
      }
      //die;
      
      return true;    
  }
  else
  {
      if(version_compare(phpversion(), "5.2.0", "<"))
      $infoVersion="(use PHP 5.2.0 or later)";
     
      echo "You need to install/enable the php_zip.dll extension $infoVersion";
  }
}
function create_dirs($path)
{
  if (!is_dir($path))
  {
    $directory_path = "";
    $directories = explode("/",$path);
    array_pop($directories);
  
    foreach($directories as $directory)
    {
      $directory_path .= $directory."/";
      if (!is_dir($directory_path))
      {
        mkdir($directory_path);
        chmod($directory_path, 0777);
      }
    }
  }
}

unzip("../widgets/".$_FILES['uploadedfile']['name'], false, true, true);
?>
<form name="unzip" action="" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    
    <input type="file" name="uploadedfile" />
    <input type="submit" value="Cài đặt" />    	
</form>